package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import linter.linterjava.POJO.DO.ArticleReview;
import linter.linterjava.POJO.DO.ReviewLikes;
import linter.linterjava.POJO.DTO.ReviewLikesDTO;
import linter.linterjava.mapper.ArticleReviewMapper;
import linter.linterjava.mapper.ReviewReportMapper;
import linter.linterjava.service.ReviewLikesService;
import linter.linterjava.mapper.ReviewLikesMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【review_likes(评论点赞表)】的数据库操作Service实现
* @createDate 2024-07-07 12:16:06
*/
@Service
public class ReviewLikesServiceImpl extends ServiceImpl<ReviewLikesMapper, ReviewLikes>
    implements ReviewLikesService{

    @Resource
    ArticleReviewMapper articleReviewMapper;
    @Resource
    ReviewLikesMapper reviewLikesMapper;
    @Resource
    ReviewReportMapper reviewReportMapper;

    @Override
    public String likeReview(Long uuid, ReviewLikesDTO reviewLikesDTO) {
        // 1. 判断是否点赞了 2. 点赞的评论是否存在 3. 点赞的评论是否被举报？ 4.被回复的评论+1
        Date date = new Date();
        ReviewLikes reviewLikes = new ReviewLikes();
        ArticleReview isExistList = articleReviewMapper.selectById(reviewLikesDTO.getReviewId());
        if (isExistList == null){ return COMMENT_NOT_EXIST; }
        if (reviewReportMapper.selectById(reviewLikesDTO.getReviewId())!= null){
            Integer isReport = reviewReportMapper.selectById(reviewLikesDTO.getReviewId()).getResultOfHanding();
            if ( isReport == null){ isReport = 0; }
            if (isReport == 1 && isReport == 2){ return COMMENT_REPORTED; }
        }
        if (reviewLikesMapper.selectOne(new QueryWrapper<ReviewLikes>().eq("user_id",uuid)) != null){ return COMMENT_IS_LIKE; }
        reviewLikes.setReviewId(reviewLikesDTO.getReviewId());
        reviewLikes.setUserId(uuid);
        reviewLikes.setCreateTime(date);
        reviewLikes.setUpdataTime(date);
        int flag = reviewLikesMapper.insert(reviewLikes);
        ArticleReview articleReview = new ArticleReview();
        Integer reviewLikeNub = articleReviewMapper.selectById(reviewLikesDTO.getReviewId()).getReviewLikeNub();
        articleReview.setReviewLikeNub(reviewLikeNub + 1);
        articleReview.setUpdataTime(date);
        flag += articleReviewMapper.update(articleReview, new QueryWrapper<ArticleReview>()
                .eq("id", reviewLikesDTO.getReviewId()).eq("is_delete", 0));
        return flag > 0 ? LIKES_SUCCESS : LIKES_ERROR;
    }
}




